<template>
	<view class="form">
		<view class="fromPadding">
			<u--form labelPosition="top" :model="uForm" :rules="rules" ref="uFormRef" labelWidth="150">
				<!-- 联系人 -->
				<view class="card">
					<text class="label">联系人信息</text>
					<u-form-item label="联系人" prop="contacts" ref="item1" required>
						<u--input v-model="uForm.contacts" placeholder="请输入联系人姓名" clearable maxlength="20"></u--input>
					</u-form-item>
					<u-form-item label="联系人电话" prop="contacts_phone" type="number" ref="item1" required>
						<u--input v-model="uForm.contacts_phone" placeholder="请输入联系人电话" clearable
							maxlength="11"></u--input>
					</u-form-item>
					<u-form-item label="联系人身份证号" prop="contacts_card" type="householder_face" ref="item1" required>
						<u--input v-model="uForm.contacts_card" placeholder="请输入联系人身份证号" clearable
							maxlength="18"></u--input>
					</u-form-item>
				</view>

				<!-- 户主 -->
				<view class="card">
					<text class="label">上传户主身份证</text>
					<view class="itemRow">
						<view class="upload _width">
							<view class="uploadImg">
								<up-upload :fileList="fileList1" @afterRead="afterRead1" @delete="deletePic1" name="1"
									:key="fileList1.length" :previewFullImage="false" :maxCount="1">
									<div class="row">
										<div class="upload_custom">
											<image
												src="https://img.zhizhuangtong.com/project_media/img/xcx/khd/idcard_1.png"
												mode="" style="" class="icon">
											</image>
											<span class="uploadTips">点击上传人像面</span>
										</div>
									</div>
								</up-upload>
							</view>
							<view class="uploadImg">
								<up-upload :fileList="fileList2" @afterRead="afterRead2" @delete="deletePic2" name="2"
									:key="fileList2.length" :previewFullImage="false" :maxCount="1">

									<div class="row">
										<div class="upload_custom">
											<image
												src="https://img.zhizhuangtong.com/project_media/img/xcx/khd/idcard_2.png"
												mode="" style="" class="icon">
											</image>
											<view class="uploadTips">
												点击上传国徽面
											</view>
										</div>
									</div>
								</up-upload>
							</view>
						</view>
					</view>


					<u-form-item label="户主姓名" type="householder_face" ref="item1" required>
						<u--input v-model="uForm.householder_person" placeholder="请输入户主姓名" clearable
							maxlength="18"></u--input>
					</u-form-item>


					<u-form-item label="户主身份证号" type="householder_face" ref="item1" required>
						<u--input v-model="uForm.householder_card" placeholder="请输入户主身份证号" clearable
							maxlength="18"></u--input>
					</u-form-item>



					<u-form-item label="身份证有效期" type="number" ref="item1" required>
						<u--input v-model="uForm.cardtime" placeholder="请输入身份证有效期" clearable></u--input>
					</u-form-item>


					<view class="map">
						<u-form-item label="户主家庭住址" prop="cardtime" type="number" ref="item1" required>
							<view class="checkAddress" @tap="changeSelectMap">地图选址</view>
						</u-form-item>
						<up-textarea v-model="uForm.address" placeholder="请输入户主家庭住址" clearable maxlength="100"
							@blur="saveLatestAddress" v-if="uForm.location"></up-textarea>

					</view>

					<u-form-item label="户主用电户号" prop="electric_num" type="householder_face" ref="item1" required>
						<u--input v-model="uForm.electric_num" placeholder="请输入户主用电户号" clearable
							maxlength="18"></u--input>
					</u-form-item>


					<u-form-item label="户主手机号" prop="householder_phone" type="householder_face" ref="item1" required>
						<u--input v-model="uForm.householder_phone" placeholder="请输入户主手机号" clearable
							maxlength="11"></u--input>
					</u-form-item>


					<view class="cards">
						<view class="label">
							<text class="title">上传户主资产证明</text>
						</view>

						<view class="upload_customRow">
							<view class="uploadCardImg">
								<view class="uploadImg">
									<up-upload :fileList="fileList3" @afterRead="afterRead3" @delete="deletePic3"
										:previewFullImage="false" name="3" :key="fileList3.length" :maxCount="6">
									</up-upload>
								</view>
							</view>
						</view>
					</view>
				</view>
			</u--form>
		</view>
		<view class="btn">
			<button @click="submit" type="primary" v-if="!uForm.code">保存</button>
			<button @click="postData" type="primary" v-else>保存</button>
		</view>
		<PhoneCode ref="phoneCodeRef" @saveCde="updateSaveCde"></PhoneCode>
	</view>
</template>
<script setup>
	import PhoneCode from '../../../components/common/phoneCode.vue' //顶部搜索组件
	import {
		ref,
		onMounted,
		nextTick,
		toRaw
	} from 'vue'
	import {
		onLoad,
		onShow,
		onUnload,
		onReady
	} from "@dcloudio/uni-app";
	import {
		orc_id,
		basic,
		card_validate,
		get_oss_url,
	} from '@/api/warm.js'

	import {
		mapGetInfo,
	} from '@/api/require.js'

	const phoneCodeRef = ref(null)
	// 数据
	const uForm = ref({
		contacts: '', //联系人  
		contacts_phone: '', //联系人电话  
		contacts_card: '', //联系人身份证号  
		householder_face: '', //户主头像面  
		householder_back: '', //户主国徽面  

		householder_person: '', //	户主姓名
		householder_card: '', //	户主身份证号

		cardtime: '', //身份证有效期
		householder_card_from: '', //户主身份证开始时间,格式2024.06.01  
		householder_card_to: '', //户主身份证到期时间,长期有效为2999.12.31  
		householder_phone: '', //户主电话 

		code: '', //户主电话验证码  

		address: '', //组装详细地址

		province: '', //省code  
		city: '', //市code  
		county: '', //区code  
		address: '', //详细地址  
		location: '', //经纬度  
		electric_num: '', //电户号  

		certs: '', //房产证  
	})
	const from = ref('')
	const to = ref('')
	// 验证规则
	const rules = {
		contacts: [{
				required: true,
				message: '请输入联系人姓名',
				trigger: ['blur', 'change']
			},
			{
				validator: (rule, value, callback) => {
					if (/[^a-zA-Z\u4e00-\u9fa5]/.test(value)) {
						return callback(new Error('联系人姓名只能包含字母和汉字'));
					}
					callback();
				},
				message: '联系人姓名格式不正确',
				trigger: ['blur', 'change']
			},
			{
				validator: (rule, value, callback) => {
					if (value && value.length > 20) {
						return callback(new Error('联系人姓名不能超过20个字'));
					}
					callback();
				},
				message: '联系人姓名不能超过20个字',
				trigger: ['blur', 'change']
			}
		],
		contacts_phone: [{
				required: true,
				message: '请输入联系人电话',
				trigger: ['blur', 'change']
			},
			{
				validator: (rule, value, callback) => {
					if (!/^\d{11}$/.test(value)) {
						return callback(new Error('联系人电话格式不正确'));
					}
					callback();
				},
				message: '请输入有效的11位手机号',
				trigger: ['blur', 'change']
			}
		],
		contacts_card: [{
				required: true,
				message: '请输入联系人身份证号',
				trigger: ['blur', 'change']
			},
			{
				validator: (rule, value, callback) => {
					// 基本格式验证 18位身份证
					const idCardPattern = /^\d{17}[\dXx]$/;
					if (!idCardPattern.test(value)) {
						return callback(new Error('身份证号格式不正确'));
					}

					// 校验出生日期
					const birthDate = value.slice(6, 14); // 截取出生日期部分 (YYYYMMDD)
					const year = parseInt(birthDate.slice(0, 4), 10);
					const month = parseInt(birthDate.slice(4, 6), 10);
					const day = parseInt(birthDate.slice(6, 8), 10);

					if (!isValidDate(year, month, day)) {
						return callback(new Error('身份证号出生日期无效'));
					}

					// 校验18位身份证的校验位
					if (!checkIdCardCheckDigit(value)) {
						return callback(new Error('身份证号校验位不正确'));
					}

					callback(); // 校验通过
				},
				message: '身份证号格式不正确',
				trigger: ['blur', 'change']
			}
		],


		householder_person: [{
				required: true,
				message: '请输入户主姓名',
				trigger: ['blur', 'change']
			},
			{
				validator: (rule, value, callback) => {
					if (/[^a-zA-Z\u4e00-\u9fa5]/.test(value)) {
						return callback(new Error('户主姓名只能包含字母和汉字'));
					}
					callback();
				},
				message: '户主姓名格式不正确',
				trigger: ['blur', 'change']
			}
		],

		electric_num: [{
				required: true,
				message: '请输入电户号',
				trigger: ['blur', 'change']
			},
			{
				validator: (rule, value, callback) => {
					if (!/^\d+$/.test(value)) {
						return callback(new Error('电户号只能输入数字'));
					}
					callback();
				},
				message: '电户号只能输入数字',
				trigger: ['blur', 'change']
			}
		],
		householder_phone: [{
				required: true,
				message: '请输入户主手机号',
				trigger: ['blur', 'change']
			},
			{
				validator: (rule, value, callback) => {
					if (!/^\d{11}$/.test(value)) {
						return callback(new Error('手机号格式不正确'));
					}
					callback();
				},
				message: '请输入有效的11位手机号',
				trigger: ['blur', 'change']
			}
		]
	}


	// 验证日期有效性
	const isValidDate = (year, month, day) => {
		const date = new Date(year, month - 1, day);
		return date.getFullYear() === year && date.getMonth() + 1 === month && date.getDate() === day;
	}

	// 校验身份证校验位（18位）
	const checkIdCardCheckDigit = (idCard) => {
		const coefficients = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
		const checkDigits = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];

		let sum = 0;
		for (let i = 0; i < 17; i++) {
			sum += parseInt(idCard[i], 10) * coefficients[i];
		}

		const checkDigit = checkDigits[sum % 11];
		return checkDigit === idCard[17].toUpperCase();
	}




	// 引用
	// const uToastRef = ref(null)
	const uFormRef = ref(null)
	const fileList1 = ref([]); //户主身份证 正面
	const fileList2 = ref([]); //户主身份证 背面
	const fileList3 = ref([]); //户主资产证明

	//获取经纬度信息
	const location = ref('')
	onLoad(() => {
		location.value = ''
		uForm.value.address = ''
		uni.setStorageSync('location', '')
		uni.setStorageSync('locationName', '')
		uni.setStorageSync('address', '')
	})
	onReady(() => {
		uni.setStorageSync('location', '')
		uni.setStorageSync('locationName', '')
		uni.setStorageSync('address', '')
		location.value = ''
		uForm.value.address = ''
	})
	onShow(() => {
		uni.setStorageSync('isUpload', '1'); //清除上传状态
		location.value = uni.getStorageSync('location') || ''
		uForm.value.location = location.value

		const isSave = uni.getStorageSync('isSave')
		console.log('isSaveisSaveisSaveisSaveisSave', isSave)
		if (isSave == 1) {
			// console.log('选择地址～～～～～～～')
			getLocalInfo()
		} else {
			const address = uni.getStorageSync('address')
			uForm.value.address = address
			// console.log('非选择地址', address)
		}


	})

	// 设置规则
	onMounted(() => {
		uni.setStorageSync('isSave', 0)
		nextTick(() => {
			if (uFormRef.value) {
				uFormRef.value.setRules(rules)
			}
		})
	})

	// 手动修改了
	const saveLatestAddress = (val) => {
		uni.setStorageSync('isSave', 0)
		uni.setStorageSync('address', val.detail.value)
		uForm.value.address = val.detail.value
	}


	// 获取位置
	const changeSelectMap = () => {
		// uni.setStorageSync('isSave', )
		uni.setStorageSync('address', uForm.value.address)
		uni.navigateTo({
			url: '/pages/requireB/map/index?isDesign=1'
		})
	}



	// 获取地址
	const getLocalInfo = async () => {
		// uForm.value.address = ''
		let obj = {
			location: location.value,
		}
		const res = await mapGetInfo(obj)
		if (res.code == 1) {
			uForm.value.province = res.data.res.province
			uForm.value.city = res.data.res.city
			uForm.value.county = res.data.res.county
			// uForm.value.address = res.data.res.address
			// const addressData = res.data.res.address
			uForm.value.location = location.value

			uForm.value.address = res.data.res.province_name + res.data.res.city_name + res.data.res
				.county_name +
				res.data.res.address
			uni.setStorageSync('address', uForm.value.addresss)
			// uni.setStorageSync('isSave', 0)
		} else {
			uForm.value.province = ''
			uForm.value.city = ''
			uForm.value.county = ''
			uForm.value.address = ''
			uForm.value.location = ''
			uForm.value.address
		}
	}
	// 删除正面图片
	const deletePic1 = (event) => {
		fileList1.value.splice(event.index, 1);
		uForm.value.householder_face = ''
		uForm.value.householder_person = ''
		uForm.value.householder_card = ''
	};
	// 删除背面
	const deletePic2 = (event) => {
		fileList2.value.splice(event.index, 1);
		uForm.value.cardtime = ''
		uForm.value.householder_card_from = ''
		uForm.value.householder_card_to = ''
		uForm.value.householder_back = ''
		fileList2.value = []
	};
	// 新增图片  身份证 正面
	const afterRead1 = async (event) => {
		uni.setStorageSync('isUpload', '1')
		uni.setStorageSync('isSave', 0); //清除上传状态
		let lists = [].concat(event.file);
		let fileListLen = fileList1.value.length;
		lists.map((item) => {
			fileList1.value.push({
				...item,
				status: 'uploading',
				message: '上传中',
			});
		});
		for (let i = 0; i < lists.length; i++) {
			const fileName = lists[i].url.match(/[^/]+$/)[0];
			const path = 'khdImage' + "/" + fileName //处理储存位置
			const result = await uploadFilePromise(path, lists[i].url, 1);
			let item = fileList1.value[fileListLen];
			fileList1.value.splice(fileListLen, 1, {
				...item,
				status: 'success',
				message: '',
				url: result,
			});
			fileListLen++;
		}

	};

	// 新增图片  身份证 背面
	const afterRead2 = async (event) => {
		uni.setStorageSync('isUpload', '1')
		uni.setStorageSync('isSave', 0); //清除上传状态

		// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
		let lists = [].concat(event.file);
		let fileListLen = fileList2.value.length;
		lists.map((item) => {
			fileList2.value.push({
				...item,
				status: 'uploading',
				message: '上传中',
			});
		});
		for (let i = 0; i < lists.length; i++) {
			const fileName = lists[i].url.match(/[^/]+$/)[0];
			const path = 'khdImage' + "/" + fileName //处理储存位置
			const result = await uploadFilePromise(path, lists[i].url, 2);
			let item = fileList2.value[fileListLen];
			fileList2.value.splice(fileListLen, 1, {
				...item,
				status: 'success',
				message: '',
				url: result,
			});
			fileListLen++;
		}
	};


	// 上传资产证明
	const afterRead3 = async (event) => {
		uni.setStorageSync('isUpload', '1')
		uni.setStorageSync('isSave', 0); //清除上传状态
		const MAX_SIZE = 10 * 1024 * 1024; // 10MB = 10 * 1024 * 1024 字节
		// 检查文件大小是否超过 10MB
		if (event.file.size > MAX_SIZE) {
			uni.showToast({
				title: '文件大小不能超过10MB',
				icon: 'none'
			});
			return;
		}

		// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
		let lists = [].concat(event.file);
		let fileListLen = fileList3.value.length;
		lists.map((item) => {
			fileList3.value.push({
				...item,
				status: 'uploading',
				message: '上传中',
			});
		});
		for (let i = 0; i < lists.length; i++) {
			const fileName = lists[i].url.match(/[^/]+$/)[0];
			// uForm.value.certs = lists[i].url;
			const path = 'khdImage' + "/" + fileName //处理储存位置
			const result = await uploadFilePromise(path, lists[i].url, 2, 1);
			let item = fileList3.value[fileListLen];
			fileList3.value.splice(fileListLen, 1, {
				...item,
				status: 'success',
				message: '',
				url: result,
			});
			fileListLen++;
		}
	};

	const Imgarr = ref([])

	const uploadFilePromise = async (url, fileUrl, type, certs) => {

		const resData = await get_oss_url({
			path: url
		});
		const url1 = resData.data.sign_url //上传图片接口
		wx.getFileSystemManager().readFile({
			filePath: fileUrl, // 文件路径
			encoding: 'base64', // 转换为 base64
			success: function(res) {
				const fileBlob = wx.base64ToArrayBuffer(res.data);
				wx.request({
					url: url1, // 仅为示例，非真实的接口地址
					filePath: url,
					method: 'PUT', // 使用 PUT 方法上传文件
					name: 'file',
					header: {
						"Content-Type": " "
					},
					data: fileBlob, // 上传的文件内容
					success: (res) => {
						uni.$u.toast('上传成功');
						if (!certs) {
							if (type == 1) {
								uForm.value.householder_face = resData.data.full_url
								console.log('正面', resData)
							} else {
								console.log('背面')
								uForm.value.householder_back = resData.data.full_url
							}
							orc_identify(url, type)
						} else {
							Imgarr.value.push(resData.data)
							uni.$u.toast('上传户主资产证明成功');
						}
					},
					fail: function(err) {
						// uni.$u.toast('33333', err);
					},
				})
			},
			fail: function(err) {},
		})

	};

	const toDate = ref('') //长期身份证

	// 身份证识别
	const orc_identify = async (url, type) => {
		try {
			const res = await orc_id({
				url: url
			});
			// 处理请求成功的情况
			if (res.code === 1) {
				if (type == 1) {

					if (res.data.face.idNumber) {
						uForm.value.householder_person = res.data.face.name;
						uForm.value.householder_card = res.data.face.idNumber;
						uni.$u.toast(res.msg);
						if (uForm.value.householder_card) {
							postIsUserCard() //检验身份证是的已经存在
							return
						}
						return
					}
					if (res.data.back.lengtn !== 0) {
						uForm.value.householder_person = ''
						uForm.value.householder_card = ''
						uForm.value.householder_face = ''
						fileList1.value = []
						uni.$u.toast('请上传正确的身份证正面照片');
						return
					}
				} else {
					// console.log('上传的身份证背面', res.data.back)

					if (res.data.face.idNumber) {
						uForm.value.cardtime = ''
						uForm.value.householder_card_from = ''
						uForm.value.householder_card_to = ''
						uForm.value.householder_back = ''
						fileList2.value = []
						uni.$u.toast('请上传正确的身份证背面照片');
						return
					}

					if (res.data.back.lengtn !== 0) {
						uForm.value.cardtime = res.data.back.validPeriod;
						// 如果是长期的
						if (uForm.value.cardtime.includes("长期")) {
							let regex = /^(\d{4}\.\d{2}\.\d{2})(.*)$/;
							let match = uForm.value.cardtime.match(regex);

							if (match) {
								let date = match[1]; // 提取日期部分
								let duration = match[2].trim(); // 提取剩余部分，并去除首尾空格

								// 如果是长期
								if (duration == '-长期' || duration == '长期') {
									toDate.value = '2999.12.31';
									uForm.value.householder_card_from = date; // 开始时间
									uForm.value.householder_card_to = toDate.value; // 结束时间
								}
							}
						} else {
							// 如果不是长期
							// 拆分日期范围
							let [startDate, endDate] = uForm.value.cardtime.split('-');
							// 处理日期格式
							uForm.value.householder_card_from = startDate;
							uForm.value.householder_card_to = endDate;
						}
						uni.$u.toast(res.msg);
					}
				}
				// uni.$u.toast(res.msg);
			} else {
				console.log('-----', res)
				uni.$u.toast(res.msg);

				if (type == 1) {
					uForm.value.householder_person = ''
					uForm.value.householder_card = ''
					uForm.value.householder_face = ''
					fileList1.value = []

				} else {
					uForm.value.cardtime = ''
					uForm.value.householder_back = ''
					fileList2.value = []
				}
				uni.$u.toast('识别失败,请检查身份证照片');
				// console.error('请求失败:', res); 
				// let errorMsg = res.msg || '未知错误'; 
			}
		} catch (error) {
			if (type == 1) {
				uForm.value.householder_person = ''
				uForm.value.householder_card = ''
				uForm.value.householder_face = ''
				fileList1.value = []

			} else {
				uForm.value.cardtime = ''
				uForm.value.householder_card_from = ''
				uForm.value.householder_card_to = ''
				uForm.value.householder_back = ''
				fileList2.value = []
			}
			uni.$u.toast('识别失败,请检查身份证照片');
		}
	}

	const isCard = ref('')

	// 户主身份证是否存在-校验
	const postIsUserCard = async () => {
		const res = await card_validate({
			householder_card: uForm.value.householder_card
		});
		if (res.code === 1) {
			// 是否存在:1=存在,0=不存在  
			if (res.data.has == 0) {
				isCard.value = res.data.has
				console.log('isCard.value', isCard.value)
				return
			} else {
				uni.$u.toast('身份证已存在');
				uForm.value.householder_person = ''
				uForm.value.householder_card = ''
				uForm.value.householder_face = ''
				fileList1.value = []
				return
			}
		} else {
			console.log('失败', res)
			uni.showToast({
				icon: 'none',
				title: res.msg
			})
		}
	}

	// 删除资产证明
	const deletePic3 = (event) => {
		fileList3.value.splice(event.index, 1);
		if (!Array.isArray(Imgarr.value)) {
			Imgarr.value = ''; // 如果不是数组，则初始化为空数组
		}
		if (Imgarr.value.length > event.index) {
			Imgarr.value.splice(event.index, 1);
		}
		console.log('删除后的图片', Imgarr.value)


	};

	// 获取验证码
	const updateSaveCde = async (code) => {
		console.log('获取到的验证码', code)
		uForm.value.code = code
		// postData()
	}

	const showToast = (message, icon = 'none') => {
		uni.showToast({
			icon: icon,
			title: message,
		});
	};

	// 户主身份证验证
	const validateHouseholderCard = (householderCard) => {
		// 基本格式验证 18位身份证
		const idCardPattern = /^\d{17}[\dXx]$/;
		if (!idCardPattern.test(householderCard)) {
			showToast('户主身份证号格式不正确');
			return false;
		}

		// 校验出生日期
		const birthDate = householderCard.slice(6, 14); // 截取出生日期部分 (YYYYMMDD)
		const year = parseInt(birthDate.slice(0, 4), 10);
		const month = parseInt(birthDate.slice(4, 6), 10);
		const day = parseInt(birthDate.slice(6, 8), 10);

		if (!isValidDate(year, month, day)) {
			showToast('户主身份证号出生日期无效');
			return false;
		}

		// 校验18位身份证的校验位
		if (!checkIdCardCheckDigit(householderCard)) {
			showToast('户主身份证号校验位不正确');
			return false;
		}

		return true;
	};

	// 提交方法
	const submit = async () => {
		// 户主手机号验证
		await nextTick();

		if (!uFormRef.value) return;

		try {
			console.log('查看表单数据', uForm.value); // 查看表单数据

			await uFormRef.value.validate();
			// 上传文件检查
			if (!uForm.value.householder_face) {
				showToast('请上传身份证正面');
				return;
			}

			if (!uForm.value.householder_back) {
				showToast('请上传身份证背面照片');
				return;
			}
			if (!uForm.value.householder_card) {
				showToast('请输入户主人身份证号');
				return;
			} else {
				const householderCard = uForm.value.householder_card;
				if (!validateHouseholderCard(householderCard)) {
					return; // 错误信息已经在 validateHouseholderCard 中处理过
				}
			}
			if (!uForm.value.cardtime) {
				showToast('请输入身份证有效期');
				return;
			}
			if (!uForm.value.address) {
				showToast('请选择家庭住址');
				return;
			}
			// 设置身份证有效期
			if (from.value && to.value) {
				uForm.value.householder_card_from = from.value;
				uForm.value.householder_card_to = to.value;
			}

			// 必填项检查
			if (!uForm.value.householder_person || !uForm.value.householder_card || !uForm.value
				.householder_card ||
				!uForm.value.householder_face || !uForm.value.householder_back) {
				showToast('请完善身份信息');
				return;
			}
			// 身份证有效期调整
			if (uForm.value.householder_card_to === '长期') {
				uForm.value.householder_card_to = '2999.12.31';
				uForm.value.householder_card = `${uForm.value.from}-长期`;
				return;
			}
			// 户主手机号验证
			if (uForm.value.householder_phone) {
				const phoneReg = /^1[3-9]\d{9}$/;
				if (!phoneReg.test(uForm.value.householder_phone)) {
					showToast('请输入正确的手机号格式');
					return;
				}


			} else {
				showToast('请填写户主手机号内容');
				return;
			}

			uForm.value.certs = Imgarr.value.map(file => file.full_url).join(',');
			if (Imgarr.value.length === 0) {
				uni.showToast({
					icon: 'none',
					title: '请上传户主资产证明'
				});
				return;
			}

			// 户主手机号验证
			if (!uForm.value.code) {
				if (phoneCodeRef.value) {
					phoneCodeRef.value.showModel(uForm.value.householder_phone);
				}
				showToast('请填写户主验证码');
				return;
			}

			return
		} catch (err) {
			showToast('请完善内容');
		}
	}
	// 保存数据
	const postData = async () => {
		let params = {
			contacts: uForm.value.contacts,
			contacts_phone: uForm.value.contacts_phone,
			contacts_card: uForm.value.contacts_card,
			householder_face: uForm.value.householder_face,
			householder_back: uForm.value.householder_back,
			householder_person: uForm.value.householder_person,
			householder_card: uForm.value.householder_card,
			cardtime: uForm.value.cardtime,
			householder_card_from: uForm.value.householder_card_from,
			householder_card_to: uForm.value.householder_card_to,
			householder_phone: uForm.value.householder_phone,
			code: uForm.value.code,
			// address: uForm.value.address,
			province: uForm.value.province,
			city: uForm.value.city,
			county: uForm.value.county,
			address: uForm.value.address,
			location: uForm.value.location,
			electric_num: uForm.value.electric_num,
			certs: uForm.value.certs,
		}
		console.log('上传的信息', params)

		// 提交请求
		const res = await basic(params)
		if (res.code === 1) {
			uni.showToast({
				icon: 'success',
				title: '提交成功'
			})
			setTimeout(() => {
				uni.navigateTo({
					url: '/pages/common/index'
				})
			}, 50)
		} else {
			uForm.value.code = ''
			showToast(res.msg);
		}
	}

	onUnload(() => {
		// uni.setStorageSync('isUpload', ''); //清除上传状态
		uni.setStorageSync('location', '')
		uni.setStorageSync('locationName', '')
		uni.setStorageSync('address', '')
		// uni.setStorageSync('isSave', 0)
		location.value = ''
		uForm.value.address = ''
	});
</script>
<style lang="less" scoped>
	.form {
		padding: 20rpx;
		min-height: 100vh;
		padding-bottom: calc(30rpx + env(safe-area-inset-bottom));
		padding-bottom: calc(30rpx + constant(safe-area-inset-bottom));

		::v-deep .u-form-item {
			.u-form-item__body__right__message {
				text-align: right;
				padding-right: 20rpx;
				padding-bottom: 10rpx;
				box-sizing: border-box;
			}

			.u-form-item__body {
				display: flex;
				flex-direction: row !important;
				padding: 0 !important;
				border-top: 1rpx solid #E0E0E0;
				box-sizing: border-box;

				.u-form-item__body__left {
					height: 90rpx;

					.u-form-item__body__left__content__required {
						left: 0 !important;
					}

					.u-form-item__body__left__content__label {
						padding-left: 20rpx;
					}
				}

				.u-form-item__body__right {
					display: flex;
					height: 90rpx;

					.u-form-item__body__right__content__slot {
						display: flex;

						.u-input {
							border: none !important;
						}

						.u-input__content__field-wrapper {
							input {
								text-align: right !important;
							}

							input-placeholde {
								color: red ! !important;
							}
						}


					}


				}
			}
		}

		.map {
			display: flex;
			flex-wrap: wrap;

			::v-deep .u-textarea {
				width: 100%;
				// border: none !important;
				margin-bottom: 20rpx;
			}

			::v-deep .u-form-item {
				width: 100%;

				.u-form-item__body {
					.u-form-item__body__left {
						height: 90rpx;

						.u-form-item__body__left__content__required {
							left: 0 !important;
						}

						.u-form-item__body__left__content__label {
							padding-left: 20rpx;
						}
					}

					.u-form-item__body__right {
						display: flex;
						height: 90rpx;

						.u-form-item__body__right__content__slot {
							display: flex;
							justify-content: flex-end;
							align-items: center;
							height: 90rpx;

							.checkAddress {
								width: 150rpx;
								height: 100%;
								padding-right: 20rpx;
								display: flex;
								justify-content: flex-end;
								align-items: center;
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #CCA05F;
								text-align: right;
								font-style: normal;
								box-sizing: border-box;
							}


						}


					}
				}
			}


		}

		.card {
			background-color: #fff;
			border-radius: 10rpx;
			padding: 0 25rpx 10rpx;
			margin-bottom: 20rpx;
			box-sizing: border-box;

			.label {
				width: 100%;
				height: 90rpx;
				line-height: 90rpx;
				display: block;
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: #333333;
				text-align: left;
				font-style: normal;


			}

			.itemRow {
				display: flex;

				.upload {
					display: flex;
					height: 226rpx;
					margin-bottom: 26rpx;
					padding-top: 30rpx;
					border-top: 1rpx solid #E0E0E0;
					box-sizing: border-box;

					.uploadImg {
						flex: 1;
						height: 100%;
						position: relative;
						overflow: hidden;
						box-shadow: rgba(99, 99, 99, 0.2) 0px 5rpx 10rpx 0rpx;
						border-radius: 10rpx;
						overflow: hidden;

						image {
							width: 100%;
							height: 100%;
							// width: 310rpx;
							// height: 206rpx;
						}

						&:last-child {
							margin-left: 46rpx;
						}

						.row {
							width: 100% !important;
							height: 100% !important;
							position: absolute;

							.upload_custom {
								width: 100% !important;
								height: 100% !important;
								box-sizing: border-box;
								position: relative;

								.icon {
									width: 100%;
									height: 100%;
								}

								.uploadTips {
									position: absolute;
									left: 0;
									bottom: 22rpx;
									text-align: center;
									width: 100%;
									font-family: PingFangSC, PingFang SC;
									font-weight: 500;
									font-size: 30rpx;
									color: #5b5b5b;
									line-height: 33rpx;
									font-style: normal;
								}
							}
						}



					}

					.uploadImg ::v-deep.u-upload__wrap__preview {
						image {
							width: 100% !important;
							height: 100% !important;
						}
					}

					.uploadImg ::v-deep.u-upload__wrap__preview,
					.uploadImg ::v-deep.u-upload {
						width: 100% !important;
						height: 100% !important;
						box-sizing: border-box;
					}

					.uploadImg ::v-deep.u-upload__button {
						width: 100% !important;
						height: 100% !important;
						margin: 0 !important;
						box-sizing: border-box;
					}

					.uploadTips {
						position: absolute;
						left: 0;
						bottom: 22rpx;
						text-align: center;
						width: 100%;
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 30rpx;
						color: #5b5b5b;
						line-height: 33rpx;
						font-style: normal;
					}
				}
			}

			.upload_customRow {
				display: flex;

				::v-deep .u-form-item {
					width: 100%;
				}

				::v-deep .u-form-item__body {
					display: flex;
					flex-wrap: wrap;
					width: 100% !important;

					.u-form-item__body__left {
						width: 100% !important;
					}

					.u-form-item__body__right {
						display: flex;
						height: 210rpx;
						padding-bottom: 20rpx;

						// 上传身份证
						.uploadCardImg {
							width: 100%;

							.uploadImg {
								width: 310rpx;
								height: 206rpx;
								position: relative;
								overflow: hidden;
								box-shadow: rgba(99, 99, 99, 0.2) 0px 5rpx 10rpx 0rpx;
								border-radius: 10rpx;
								overflow: hidden;

								image {
									width: 100%;
									height: 100%;
									// width: 310rpx;
									// height: 206rpx;
								}

								.row {
									width: 100% !important;
									height: 100% !important;
									position: absolute;
									width: 310rpx;
									height: 210rpx;

									.upload_custom {
										width: 100% !important;
										height: 100% !important;
										box-sizing: border-box;
										position: relative;

										.icon {
											width: 100%;
											height: 100%;
										}

										.uploadTips {
											position: absolute;
											left: 0;
											bottom: 22rpx;
											text-align: center;
											width: 100%;
											font-family: PingFangSC, PingFang SC;
											font-weight: 500;
											font-size: 30rpx;
											color: #5b5b5b;
											line-height: 33rpx;
											font-style: normal;
										}
									}
								}
							}

						}


					}
				}


			}


			// 上传房主资产
			.cards {
				display: flex;
				flex-wrap: wrap;

				.title {
					width: 100%;
					font-family: PingFangSC, PingFang SC;
					font-weight: 500;
					font-size: 30rpx;
					color: #333333;
					text-align: left;
					font-style: normal;
					position: relative;
					display: block;
					padding-left: 10rpx;
					box-sizing: border-box;

					&::before {
						content: '*';
						position: absolute;
						left: -10rpx;
						top: 0rpx;
						color: red;
					}
				}

				.upload_customRow {
					display: flex;
					width: 100%;

					.uploadCardImg {
						width: 100%;
						padding-bottom: 20rpx;
						box-sizing: border-box;

						.uploadImg {
							width: 100%;
							position: relative;
							// overflow: hidden;

							display: flex;
							flex-wrap: wrap;

							::v-deep .u-upload {
								display: flex;
								flex-wrap: wrap;
								min-height: 240rpx;

								.u-upload__button {
									width: 49% !important;
									height: 200rpx !important;
									box-sizing: border-box;
									margin: 0 !important;
								}

								.u-upload__deletable {
									right: 12rpx;
									top: 1rpx;
									z-index: 2;
								}

								.u-upload__success {
									right: 12rpx;
									bottom: 22rpx;
									z-index: 2;
								}

								.u-upload__wrap__preview {
									width: 50%;
									height: 220rpx;
									margin: 0 !important;
									padding: 0 10rpx 20rpx 0 !important;
									border-radius: 10rpx;
									box-sizing: border-box;

									image {
										width: 100% !important;
										height: 100% !important;
										border: 1px solid #E0E0E0;
										background-color: aliceblue;
										border-radius: 10rpx;
										box-shadow: rgba(99, 99, 99, 0.2) 0px 5rpx 10rpx 0rpx;
										box-sizing: border-box;
									}
								}
							}
						}

					}
				}
			}

		}
	}

	.btn {
		width: 100%;
		display: flex;
		justify-content: center;
		padding: 30rpx;
		box-sizing: border-box;
		// position: fixed;
		// left: 0;
		// z-index: 2;
		// bottom: constant(safe-area-inset-bottom);
		// bottom: env(safe-area-inset-bottom);
		// bottom: calc(120rpx + env(safe-area-inset-bottom));
		// bottom: calc(120rpx + constant(safe-area-inset-bottom));

		button {
			width: 100%;
			height: 88rpx;
			background: linear-gradient(138deg, #EDA55B 0%, #D09A47 100%);
			border-radius: 14rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
			line-height: 88rpx;
			font-style: normal;
		}

	}
</style>